//
//  LocationViewController.m
//  Project_2_weibo
//
//  Created by 林林 on 16/6/25.
//  Copyright © 2016年 lhn. All rights reserved.
//

#import "LocationViewController.h"
#import <CoreLocation/CoreLocation.h>
#import "UIImageView+WebCache.h"

@interface LocationViewController () <CLLocationManagerDelegate, UITableViewDelegate, UITableViewDataSource, SinaWeiboRequestDelegate>
{
    UITableView *_tbView;
    CLLocationManager *_locationManager;
    NSArray *_locationArr;
}

@end

@implementation LocationViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self createNavBarButton];
    [self createTableView];
    [self openLocation];
}

- (void)createNavBarButton {
    
    //取消
    ThemeButton *backButton = [ThemeButton buttonWithType:UIButtonTypeCustom];
    backButton.frame = CGRectMake(0, 0, 60, 44);
    backButton.backgroundImageName = @"titlebar_button_9.png";
    [backButton setTitle:@"取消" forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(backAction:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    self.navigationItem.leftBarButtonItem = backItem;
}

- (void)backAction:(UIButton *)button {
    
    if (self.navigationController) {
        [self.navigationController popViewControllerAnimated:YES];
    }else {
        [self dismissViewControllerAnimated:YES completion:nil];
    }
}

#pragma mark - uitableView
- (void)createTableView {
    
    _tbView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - 64) style:UITableViewStylePlain];
    _tbView.backgroundColor = [UIColor clearColor];
    _tbView.delegate = self;
    _tbView.dataSource = self;
    [self.view addSubview:_tbView];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _locationArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (cell == nil) {
        
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    
    NSDictionary *dic = _locationArr[indexPath.row];
    cell.textLabel.text = dic[@"title"];
    id address = dic[@"address"];
    if (![address isKindOfClass:[NSNull class]]) {
        
        cell.detailTextLabel.text = dic[@"address"];
    }
    [cell.imageView sd_setImageWithURL:[NSURL URLWithString:dic[@"icon"]]];
    cell.backgroundColor = [UIColor clearColor];
    
    return cell;
}

#pragma mark - location
- (void)openLocation {
    
    _locationManager = [[CLLocationManager alloc] init];
    if (kSystemVersion >= 8.0) {
        [_locationManager requestWhenInUseAuthorization];
    }
    /*
     在plist文件加入NSLocationWhenInUseUsageDescription
     */
    _locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters;
    _locationManager.delegate = self;
    
    [_locationManager startUpdatingLocation];
    
}

#pragma mark - CLLocationManagerDelegate
- (void)locationManager:(CLLocationManager *)manager
     didUpdateLocations:(NSArray<CLLocation *> *)locations {
    //关闭定位
    [manager stopUpdatingLocation];
//    NSLog(@"%@", locations);
    //获取经纬度
    CLLocation *location = [locations firstObject];
    CGFloat lon = location.coordinate.longitude;
    CGFloat lat = location.coordinate.latitude;
    //用新浪微博的接口,进行位置信息的反编码
    SinaWeibo *weibo = kWeiboObject;
    NSMutableDictionary *params = [@{@"long" : [NSString stringWithFormat:@"%f", lon],
                                     @"lat" : [NSString stringWithFormat:@"%f", lat]
                                    } mutableCopy];
    [weibo requestWithURL:@"place/nearby/pois.json" params:params httpMethod:@"GET" delegate:self];
    
}

#pragma mark - SinaWeiboRequestDelegate
- (void)request:(SinaWeiboRequest *)request didFinishLoadingWithResult:(id)result {
    
    _locationArr = result[@"pois"];
    [_tbView reloadData];
}

#pragma mark -
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    //获取选中的位置
    NSDictionary *location = _locationArr[indexPath.row];
    //执行block回调
    if (_block) {
        _block(location);
    }
    [self.navigationController popViewControllerAnimated:YES];
}


#pragma mark -
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end





